#ifndef HJ212_H
#define HJ212_H

#include <QObject>
#include <QString>
#include <QByteArray>
#include <QMetaEnum>
#include <QDateTime>
#include "devicelist.h"
#include "hostlist.h"
#include "stationmodel.h"



#define dataFlagNormal 'N'//数据标记N,在线监控仪器仪表工作正常
#define dataFlagStopRunning 'F'//数据标记F,在线监控仪器仪表停运
#define dataFlagMaintenance 'M'//数据标记M,在线监控仪器仪表处理维护期间
#define dataFlagUserInput 'S'//数据标记S,数据为手工输入的设定值
#define dataFlagBreakDown 'D'//数据标记D,在线监控仪器仪表故障
#define dataFlagCalibration 'C'//数据标记C,在线监控仪器仪表处理校准状态
#define dataFlagTransfinite 'T'//数据标记T,在线监控仪器仪表采样值超限
#define dataFlagCommError 'B'//数据标记B,在线监控仪器仪表通讯异常


namespace Moolink {


    class HJ212_DATA
    {
    public:
        QString QN;
        int ST;
        int CN;
        QString PW;
        QString MN;
        qint8 Flag;
        QString PNUM;
        QString PNO;
        QString CP17;
        QString CP05;
        HJ212_DATA() {}
    //    HJ212_DATA(QString QN_,QString ST_,QString CN_,QString PW_,QString MN_,unsigned char Flag_,QString PNUM_,QString PNO_,QString CP_)
    //    {
    //        //赋值
    //        QN=QN_;ST=ST_;CN=CN_;PW=PW_;MN=MN_;Flag=Flag_;PNUM=PNUM_;PNO=PNO_;CP=CP_;
    //        //更新datastr

    //    }
    //    HJ212_DATA(QString dataStr)
    //    {

    //    }


        //请求编码QN,系统编码ST,命令编码CN,访问密码PW,设备标识MN,拆包及应答Flag,总包数PNUM,包号PNO,指令参数CP
    //    QString getDataString()
    //    {
    //        QString data="";
    //        if(0==(Flag&0x02))//无分包
    //        {
    //            //data.sprintf("QN=%s;ST=%s;CN=%s;PW=%s;MN=%s;Flag=%d;CP=&&%s&&",QN,ST,CN,PW,MN,Flag,CP);
    //        }
    //        else//有分包
    //        {

    //        }
    //        return data;
    //    }
    };


    class HJ212
    {
    public:

        //GetSend:上位机查询下位机与下位机应答或者下位机上传与上位机应答,Set:上位机设置下位机,Req:下位机向上位机请求
        enum CNenum{
            //初始化命令
            Timeout_ReSend=1000,
            //参数命令
            LowerComputerTime_GetSend=1011,
            LowerComputerTime_Set,
            LowerComputerTimeCal_Req,
            RealTimeDataInterval_GetSend=1061,
            RealTimeDataInterval_Set,
            MinuteDataInterval_GetSend,
            MinuteDataInterval_Set,
            //数据命令-实时数据
            PollutantRealTimeData_GetSend=2011,
            PollutantRealTimeData_Stop,
            //数据命令-设备状态
            DeviceStaus_GetSend=2021,
            DeviceStaus_Stop,
            //数据命令-日数据
            PollutantDayHistoryData_GetSend=2031,
            DeviceRunDayHistoryData_GetSend=2041,
            //数据命令-分钟数据
            PollutantMinuteHistoryData_GetSend=2051,
            //数据命令-小时数据
            PollutantHourHistoryData_GetSend=2061,
            //数据命令-其它数据
            DataCollectionDeviceRunTime_Send=2081,
            //控制命令
            ZeroRangeCalibration_Set=3011,
            InstantSampling_Set,
            CleaningBackblowing,
            ComparingSampling,
            ExceedingStandardSample_GetSend,
            SamplingTimePeriod_Set,
            SamplingTimePeriod_GetSend,
            SamplingTime_GetSend,
            DeviceId_GetSend,
            LowerComputerInfo_GetSend,
            LowerComputerInfo_Set,
            //交互命令
            RequestResponse=9011,
            ExecutionResults,
            NotificationResponse,
            DataResponse

        };
        enum STenum{
            SurfaceWaterQualityMonitor=21,//地表水质量监测
            AirQualityMonitor,//空气质量监测
            SoundEnvironmentQualityMonitor,//声环境质量监测
            GroundWaterQualityMonitor,//地下水质量监测
            SoilQualityMonitor,//土壤质量监测
            SeaWaterQualityMonitor,//海水质量监测
            VOCMonitor,//挥发性有机物监测
            AtmosphericPollutionSource=31,//大气环境污染源
            SurfaceWaterPollutionSource,//地表水体环境污染源
            GroundWaterPollutionSource,//地下水体环境污染源
            OceanPollutionSource,//海洋环境污染源
            SoilPollutionSource,//土壤环境污染源
            SoundPollutionSource,//声环境污染源
            VibrationPollutionSource,//振动环境污染源
            RadioactivePollutionSource,//放射性环境污染源
            RaiseDustPollutionSource,//工地扬尘污染源
            ElectromagneticPollutionSource=41,//电磁环境污染源
            FlueGasEmissionProcessMonitor=51,//烟气排放过程监控
            SewageDischargeProcessMonitor,//污水排放过程监控
            SystemInteraction=91//系统交互
        };

        enum dataFlag{
            N=0,//数据标记N,在线监控仪器仪表工作正常
            F,//数据标记F,在线监控仪器仪表停运
            M,//数据标记M,在线监控仪器仪表处理维护期间
            S,//数据标记S,数据为手工输入的设定值
            D,//数据标记D,在线监控仪器仪表故障
            C,//数据标记C,在线监控仪器仪表处理校准状态
            T,//数据标记T,在线监控仪器仪表采样值超限
            B//数据标记B,在线监控仪器仪表通讯异常
        };


        static QString formatNumber(QString format, QVariant num);
        static bool recvCN1000(QString &qstrSrc,StationModel &object);
        static QString recvCN1011(QString &qstrSrc);
        static bool recvCN1012(QString &qstrSrc,Device &object);
        static QString recvCN1062(QString &qstrSrc);

        static QString GetCpValueByKey(QString &qstrSrc,QString key);

        //static QString ResponseData(HJ212_DATA &object,QString cpStr,int Flag=4);
        static QString ResponseData(HJ212_DATA &object,QString cpStr);
        static QString ResponseData(HJ212_DATA &object);
        static QString ReqReponse(HJ212_DATA &object,int QnRtn=1);
        static QString ReqResult(HJ212_DATA &object,int ExeRtn=1);

        static QString getDataPacket(QString msgtext);
        //static int getDataObject(QString recvmsg,HJ212_DATA &object,StationModel &station);//QString stationMN,QString stationPW);
        static int getDataObject(QString recvmsg,HJ212_DATA &object,Host &host);
        static QString getQN();
        static QString getDataTime();
        static qint8 getFlag(qint8 protocolFlag,bool needReply=false,bool packetMore=false){if(needReply) protocolFlag|=0x01; if(packetMore) protocolFlag|=0x01; return protocolFlag;}
        static unsigned int CRC16_Checkout (const QString Str, unsigned int usDataLen );
        static bool IsNumber(QString &qstrSrc);
        static bool IsHex(QString &qstrSrc);
    };
}
#endif // HJ212_H
